<template>
    <div class="app-container">
        <el-form :model="studyType" :rules="rules" ref="ruleForm" label-width="200px" class="demo-ruleForm">
            <el-form-item label="课程类型" prop="typeName" >
                <el-input v-model="studyType.typeName"></el-input>
            </el-form-item>
            <el-form-item label="排序编码" prop="orderNum" >
              <el-input v-model="studyType.orderNum"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
                <el-button @click="resetForm('ruleForm')">重置</el-button>
                <el-button @click="returnPage()">返回</el-button>
            </el-form-item>
        </el-form>
    </div>
    
</template>

<script>
import { addStudyTypeApi } from "@/api/studyType"
  export default {
    data() {
      return {
        studyType: {},
        rules: {
        }
      }
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            var formdata = new FormData()
            Object.keys(this.studyType).forEach(key => {
              formdata.append(key, this.studyType[key])
            })
            addStudyTypeApi(formdata).then(response => {
              if(response.status == 0){
                this.$message('提交成功')
              }
              this.$router.push('/content/studyType')
            })
          } 
        })
      },
      resetForm(formName) {
        this.$refs[formName].resetFields()
      },
      returnPage() {
        if (window.history.length <= 1) {
          this.$router.push({ path: "/zh-CN/home" });
          return false;
        } else {
          this.$router.go(-1);
        }
      }
    }
  }
</script>